package com.bookStore.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bookStore.dao.AdminDao;
import com.bookStore.domain.User;

@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		// 输出流的字符编码处理，编码在web.xml配置
    	String encoding=getServletContext().getInitParameter("encoding");
        resp.setContentType("text/html;charset="+encoding);
        resp.setCharacterEncoding(encoding);
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession(true);
        
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        if(email.length() * password.length() > 0){
        	AdminDao adminDao = new AdminDao();
        	User admin = adminDao.checkLogin(email, password);
        	if(admin != null){
        		session.setAttribute("admin", admin);
        		req.getRequestDispatcher("admin.jsp").forward(req, resp);
        	}else{
        		out.println("<script>alert('对不起，用户名或密码错误，请重新再试！');location. href='http://localhost:8080/adminLogin.html';</script>");
        	}
        }else{
        	out.println("<script>alert('账号或密码为空！');location. href='http://localhost:8080/adminLogin.html';</script>");
        }
	}

}
